package com.logicwin.aibala.ui.screens.Downloads;

import java.util.List;

import com.logicwin.aibala.R;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Button;
import com.logicwin.aibala.model.androidSWInfo.APKInstallInfo;
public class ScreenDownloadApkAdapter extends BaseAdapter implements AdapterView.OnItemClickListener{
	private final int mHighestLevel = 5;
	private final int mLowestLevel  = 0;	
	protected Activity mParentActivity;
	protected List<APKInstallInfo> mApkList;
	public ScreenDownloadApkAdapter(Activity activity, List<APKInstallInfo> apkList){
		mParentActivity = activity;
		mApkList = apkList;
	}
	@Override
	public int getCount() {
		if(mApkList == null) return 0;
		
		return mApkList.size();
	}

	@Override
	public Object getItem(int position) {
		if(mApkList == null) return null;
		return mApkList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		APKInstallInfo item;
		
		if (view == null) {		
			view = mParentActivity.getLayoutInflater().inflate(R.layout.download_item, null);
		}
		if ((item = this.mApkList.get(position)) == null) {
			return view;
		}
		inflateItemView(view, item);
		return view;
	}
		
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
	}

	private void inflateItemView(View view, APKInstallInfo item){
		ImageView packimage = (ImageView) view.findViewById(R.id.download_item_image);
		
//		if(item.getIcon() != null) packimage.setImageBitmap(item.getIcon());
//		packimage.setImageBitmap(item.getIcon());
		
		((TextView) view.findViewById(R.id.download_item_package_name)).setText(item.getAPKName());
		((TextView) view.findViewById(R.id.download_item_current_version)).setText(item.getCurrVersion());
//		((TextView) view.findViewById(R.id.download_item_newest_version)).setText(item.getNewestVersion());
//		((TextView) view.findViewById(R.id.download_item_package_size)).setText(item.get);
		
		((Button) view.findViewById(R.id.download_item_button)).setOnClickListener(
				new DownloadClickListener(view,item));
//		inflateLevel(view, item.getLevel());			
	}
	
	private void inflateLevel(View view, int level){
		RatingBar bar = (RatingBar) view.findViewById(R.id.download_item_stars);
		if(level<mLowestLevel) level = mLowestLevel;
		if(level>mHighestLevel) level = mHighestLevel;
		bar.setRating(level);
	}	
	class DownloadClickListener implements OnClickListener{
		
		private View mView;
		private APKInstallInfo mApkInfo;
		private int mProgressStatus = 0;
		public DownloadClickListener(View view, APKInstallInfo apkinfo){
			mApkInfo = apkinfo;
			mView = view;
		}
		@Override
		public void onClick(View v) {
			LinearLayout layout = (LinearLayout)mView.findViewById(R.id.download_item_version_layout);
			layout.removeAllViews();
			final ProgressBar child = new ProgressBar(mParentActivity,
					null,android.R.attr.progressBarStyleHorizontal);
			child.setMax(100);
			child.setProgress(0);
			child.setLayoutParams(new LinearLayout.LayoutParams(250,LayoutParams.WRAP_CONTENT));
			layout.addView(child);
			new Thread(new Runnable() {            
				public void run() {                 
					while (mProgressStatus < 100) {                     
						mProgressStatus++;
						try {
							Thread.sleep(200);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} 
						// Update the progress bar                     
						((ScreenDownloads)mParentActivity).getHandler().post(new Runnable() {                         
							public void run() {                             
								child.setProgress(mProgressStatus);                         
								}                     
							});                 
						} 
					
					}         
				}).start(); 			
		}
		
	}
}
    